package com.acwing.lc.part1;

import com.acwing.lc.common.ListNode;

/**
 * @author rkc
 * @date 2022/9/16 15:48
 */
public class LC61旋转链表 {

    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || k == 0) return head;
        ListNode tail = head, p = head;
        int size = 1;
        while (tail.next != null) {
            tail = tail.next;
            size++;
        }
        k %= size;
        if (k == 0) return head;
        for (int i = 0; i < size - k - 1; i++) p = p.next;
        tail.next = head;
        head = p.next;
        p.next = null;
        return head;
    }
}
